use ExtUtils::MakeMaker;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.

my $PERL_BIN  = $ARGV[0] || '/usr/bin/perl';
my $INSTARGET = $ARGV[1] || '/usr/local/www/cgi-bin';
my $EXECUTABLE = "./athena.pl";

autogen_executable();
autogen_WebAPI();

if (! -e $INSTARGET) {
  safe_die("The install target does not exist!");
}

if (substr($INSTARGET, -1, 1) eq "/") {
  $INSTARGET = substr($INSTARGET, 0, -1);
}

sub autogen_executable  {
  # autogen executable

  if (open(WRITE_ATHENA, ">$EXECUTABLE")) {
    print (STDOUT "autogen: $EXECUTABLE\n");
    print (WRITE_ATHENA 
	   "#!$PERL_BIN\n\n",
	   "use Athena;\n\n",
	   "Athena::main();\n");
    close(WRITE_ATHENA);
    my $result = `chmod +x $EXECUTABLE`;
    if (scalar($result)) {
      print STDOUT "Error changing privs on executable: $result";
    }
  } else {
    safe_die("Error open executable output in build tree: $EXECUTABLE");
  }
}

sub autogen_WebAPI {
  my $dir = "lib/Athena/WebAPI";
  my @fixfiles = ("HTML.pm", "Table.pm", "Form.pm");
  while (@fixfiles) {
    my $file = pop(@fixfiles);
    my $stubfile = "stub." . $file;
    print (STDOUT "autogen in WebAPI: $stubfile -> $file\n");
    open(STUBFILE, "<$dir/$stubfile") ||
      safe_die("Catastrophe: cannot open WebAPI stub file: $stubfile");
    open(GENFILE, ">$dir/$file") ||
      safe_die("Catastrophe: cannot open WebAPI autogen file: $file");
    while (<STUBFILE>) {
      $_ =~ s/\@\@PACKAGE\@\@/Athena/g;
      print (GENFILE $_);
    }
    close(STUBFILE);
    close(GENFILE);
  }
}

sub safe_die {
  system("cp Makefile.failsafe Makefile");
  die(@_, "\nPERL MODULE WILL NOT BUILD\n");
}

WriteMakefile(
	      NAME              => 'Athena',
	      VERSION_FROM      => 'lib/Athena.pm', # finds $VERSION
	      PREREQ_PM         => {CGI=>2.0}, 
	      ($] >= 5.005 ?     
	       (ABSTRACT_FROM  => 'lib/Athena.pm', 
		AUTHOR         => 'mark wolgemuth <mark@node.to>') : ()),
	      EXE_FILES         => [ "$EXECUTABLE" ],
	      INSTALLSCRIPT     => "$INSTARGET/",
	      LIBS              => [''], # e.g., '-lm'
	      DEFINE            => '', # e.g., '-DHAVE_SOMETHING'
	      INC               => '-I. -I../../../c/include',
#	      depend            => {distclean => realclean},
);
if  (eval {require ExtUtils::Constant; 1}) {
  # If you edit these definitions to change the constants used by this module,
  # you will need to use the generated const-c.inc and const-xs.inc
  # files to replace their "fallback" counterparts before distributing your
  # changes.
  my @names = (qw(ATH_ALGO_CPU ATH_ALGO_CU0 ATH_ALGO_CU1 ATH_ALGO_CU2
		 ATH_ALGO_CU3 ATH_ALGO_CU4 ATH_ALGO_CU5 ATH_ALGO_CU6
		 ATH_ALGO_CU7 ATH_ALGO_CU8 ATH_ALGO_CU9 ATH_ALGO_DIS
		 ATH_ALGO_DYN ATH_ALGO_FIELDS ATH_ALGO_LOD ATH_ALGO_MAXEX
		 ATH_ALGO_MEM ATH_ALGO_NET ATH_ALGO_NON ATH_ALGO_ROR
		 ATH_ALGO_SIM ATH_ALGO_TYPES ATH_CASE_ADD_P ATH_CASE_ALGO
		 ATH_CASE_CHOSEN ATH_CASE_DURL ATH_CASE_ESC ATH_CASE_EXPECT
		 ATH_CASE_E_PATH ATH_CASE_FARM ATH_CASE_FMT ATH_CASE_F_MEMB
		 ATH_CASE_F_NAME ATH_CASE_HITADDS ATH_CASE_IN ATH_CASE_MTIME
		 ATH_CASE_NOMINA ATH_CASE_ON ATH_CASE_OURL ATH_CASE_PHYS
		 ATH_CASE_P_HOST ATH_CASE_P_MEMB ATH_CASE_REM_P ATH_CASE_RTTL
		 ATH_CASE_SERIAL ATH_CASE_SPC ATH_CASE_STATS ATH_CASE_TTL
		 ATH_CASE_UP ATH_CASE_URLESC ATH_DOWN ATH_EOA ATH_FARM_HARDMAX
		 ATH_MUXS_HARDMAX ATH_NAME_MAX ATH_OFF ATH_ON ATH_OPN
		 ATH_PHYS_HARDMAX ATH_READ_ONLY ATH_READ_WRITE ATH_UP));
  ExtUtils::Constant::WriteConstants(
                                     NAME         => 'Athena',
                                     NAMES        => \@names,
                                     DEFAULT_TYPE => 'IV',
                                     C_FILE       => 'const-c.inc',
                                     XS_FILE      => 'const-xs.inc',
                                  );

}
else {
  use File::Copy;
  use File::Spec;
  foreach my $file ('const-c.inc', 'const-xs.inc') {
    my $fallback = File::Spec->catfile('fallback', $file);
    copy ($fallback, $file) or die "Can't copy $fallback to $file: $!";
  }
}
